#include "../head.h"

// int pipe(int pipefd[2]);->执行这个函数之后，得到两个文件描述符
int main()
{
    int fd[2];
    // 创建两个管道 fd[0]读 fd[1]写
    pipe(fd);

    pid_t pid = fork();
    if (pid > 0)
    {
        printf("parent\n");
        while (1)
        {
            char buf[32];
            scanf("%s", buf);
            write(fd[1], buf, sizeof(buf));
            printf("parent send buf = %s\n", buf);
            if (strcmp(buf, "exit") == 0)
            {
                break;
            }
        }
        wait(NULL);
    }
    if (pid == 0)
    {
        printf("child\n");
        while (1)
        {
            char buf[32];
            read(fd[0], buf, sizeof(buf));
            printf("child accept buf = %s\n", buf);
            if (strcmp(buf, "exit") == 0)
            {
                break;
            }
        }
    }

    return 0;
}